package com.qiange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiange.common.Result;
import com.qiange.dao.SetmealDao;
import com.qiange.dto.SetmealDto;
import com.qiange.exception.CustomException;
import com.qiange.pojo.Setmeal;
import com.qiange.pojo.SetmealDish;
import com.qiange.service.SetmealDishService;
import com.qiange.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWithDish(SetmealDto setmealDto) {
        //先保存基本的菜品信息
        this.save(setmealDto);

        //再保存相应的菜品信息到菜品套餐关联表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((setmealDish) ->{
            //给集合中的套餐id赋值
            setmealDish.setSetmealId(setmealDto.getId());

            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeWithDish(List<Long> ids){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus,1);

        int count = this.count(lqw);
        if (count > 0) {
            throw new CustomException("不能删除正在售卖中的套餐");
        }


        //先删除套餐表中的数据
        this.removeByIds(ids);

        //再删除套餐菜品关联表中的数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);


    }

    @Override
    public SetmealDto selectByIdWithDish(Long id) {

        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = setmealService.getById(id);

        BeanUtils.copyProperties(setmeal,setmealDto);


        LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper();
        setmealDishLqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLqw);

        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWithDish(SetmealDto setmealDto) {

        this.updateById(setmealDto);



        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        setmealDishList = setmealDishList.stream().map((setmealDish -> {

            setmealDish.setSetmealId(setmealDto.getId());

            return setmealDish;
        })).collect(Collectors.toList());


        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(lqw);

        setmealDishService.saveBatch(setmealDishList);
    }
}
